<!DOCTYPE html>
<html>
<head>
    <title>添加药品--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>
 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
    $(function () {       
		$('#backid').click(function(){
				window.location.href="index.html";
		 });
    });
    </script>
</head>
<body>
<form action="index.html" method="post"  id="myForm" class="definewidth m20">
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td width="10%" class="tableleft">药品编号</td>
        <td><span id="drug_id">-</span></td>
    </tr> 
	
    <tr>
        <td width="10%" class="tableleft">药品名称</td>
        <td><span id="drug_name">-</span></td>
    </tr>    
	<tr>
        <td width="10%" class="tableleft">余量</td>
        <td><span id="drug_number">-</span></td>
    </tr>    
	
    <tr>
        <td width="10%" class="tableleft">数量</td>
        <td>
			<input type="text" value="300" id="addNumber"/>
		</td>
    </tr>
	

    <tr>
        <td colspan="2">
			<center>
				<button  class="btn btn-primary" type="button" id="btnUpdate">保存</button> &nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
			</center
		</td>
    </tr>
</table>
</form>
</body>
</html>
<script>
    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }
    $(function () {
        $('#backid').click(function(){
            window.location.href="index.html";
        });
        //根据地址获取Id,如果null则执行增加，否则执行更新
        let url = document.location.toString();//获取URL
        let drug_id=url.GetValue("drug_id");
        var data1 = null;
        var drug_number = null;
        queryById(drug_id);
        function queryById(drug_id) {
            $.ajax({
                type: "GET",
                url: "/drug/" + drug_id, //RESTful风格的API定义
                data: "",
                success: function (vo) {
                    let obj = vo.obj;
                    let drug_id= obj.drug_id;
                    let drug_name = obj.drug_name;
                     drug_number = obj.drug_number;
                     if(drug_number == null ||drug_number ==""){
                         drug_number=0;
                     }
                    $("#drug_id").html(drug_id);
                    $("#drug_name").html(drug_name);
                    $("#drug_number").html(drug_number);
                }
            })
        }
        $("#btnUpdate").click(function () {
            var add = $("#addNumber").val();
            data1 = parseInt(drug_number)+parseInt(add);
            $.ajax({
                type: "POST",//这里必须写POST
                url: "/drug/"+drug_id,
                data: {drug_number:data1,_method:"PUT"},
                dataType:"json",
                success: function(vo){
                    if(vo.code==200) {
                        alert("更新成功！");
                    }else{
                        alert("更新失败！"+vo.msg);
                    }
                }
            });
        });
    });
</script>